﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SwarmMappingAlpha
{
    public class mappedArray
    {
        int[,] map;
        int nodeNumber;

        int[] inTree;
        int[] distance;
        int[] shouldBeLinkedTo;
        public mappedArray(int numberOfNodes)
        {
            nodeNumber = numberOfNodes;
            map = new int[nodeNumber,numberOfNodes];
            inTree = new int[nodeNumber];
            distance = new int[nodeNumber];
            shouldBeLinkedTo = new int[nodeNumber];
        }

        public void populate(List<Edge> edgeList)
        {
            foreach(Edge vag in edgeList)
            {
                map[vag.startNode.nodeNumber, vag.endNode.nodeNumber] = vag.weight;
            }
        }

        public void updateDistance(int target)
        {
            for (int i = 0; i < nodeNumber; i++)
            {
                if ((map[target, i] != 0) && (distance[i] > map[target, i]))
                {
                    distance[i] = map[target, i];
                    shouldBeLinkedTo[i] = target;
                }
            }
        }

        public int[] MST()
        {
            //List<int> optimalPaths = new List<int>();
            for (int i = 0; i < nodeNumber; i++)
            {
                inTree[i] = 0;
                distance[i] = 100100;
            }

            inTree[0] = 1;
            updateDistance(0);

            int total = 0;
            for (int treeSize = 1; treeSize < nodeNumber; treeSize++)
            {
                int min = -1;
                for (int i = 0; i < nodeNumber; i++)
                {
                    if (inTree[i] == 0)
                        if ((min == -1) || (distance[min] > distance[i]))
                            min = i;
                    inTree[min] = 1;
                    total += distance[min];

                    updateDistance(min);
                }
            }

            return inTree;
        }

    }
}
